#include <QtGui>
#include <fcntl.h>
#include "ctrlthread.h"
#include "ctrl.h"
#include "measprog.h"

#include "../wrapper.h"
#include <sched.h>

void CtrlThread::run()
{
/*
	int key;
		
	while(1){
		if(GlobalCfg::MM_AUTO == globalCfg.getMeasMode()){
			// wait meas signal
			scanbuttons(&key);
		}else if(GlobalCfg::MM_MANUAL == globalCfg.getMeasMode()){
//			qDebug() << "wait manual signal!!!";
			while(1){
//				qDebug() << w->frmMeas->manualFlag;
				if(true == w->frmMeas->manualFlag){
					w->frmMeas->manualFlag = false;
					break;
				}
			}		
		}


	}
*/

/*
	Ctrl ct = Ctrl::instance();
	MeasProg mp = MeasProg::instance();
//	int mode = mp.getMeasMode();
*/
/*
	Ctrl* pCtrl = Ctrl::getInstance();
	pCtrl->setMeasState(true);
*/
//	MeasProg* pMeasProg = MeasProg::getInstance();
//	program_s* p = pMeasProg->getProgPtr();
//	short mmode = p->mmode;

	//
	// 设置线程优先级
	//
	struct sched_param param;
	param.sched_priority = 99;
	sched_setscheduler(0, SCHED_RR, &param);	

	measStatus = true;	
	manualPressed = false;

	initDevices();
	meas_main();
	releaseDevices();
/*
	int key;
	while(1){
		if(PM_AUTO == mmode){			// AUTO
			if(scanbuttons(&key) == -1){
				return;
			}
		}else if(PM_MANUAL == mmode){	// MANUAL
			if(waitManual() == -1){
				return;
			}
		}
		
		pCtrl->sem1_v();
		pCtrl->sem2_p();

		//showMeasData()
//		qDebug() << "measDataReady signal emitted!";
		emit measDataReady();

		if(pCtrl->getMeasState() == false){
			return;
		}
	}
*/

//	qDebug() << "!!!!!!!!!!!!!!!!!! ctrl thread !!!!!!!!!!!!!!!!!!!!!!!";
}

void CtrlThread::emitMeasDataReadySignal()
{
//	qDebug() << "all_in_on measDataReady signal!!!!!!!!";
	emit measDataReady();
}

int CtrlThread::waitManual()
{
	while(1){
		if(manualPressed){
			manualPressed = false;
			return 0;
		}

/*
		Ctrl* p = Ctrl::getInstance();
		if(!p->getMeasState()){
			return -1;
		}
*/
		if(!getMeasStatus()){
			return -1;
		}
	}	

	return 0;
}

int CtrlThread::scanbuttons(int * pckey)
{
	static char buttons[6] = { '0', '0', '0', '0', '0', '0' };
	int buttons_fd;

	buttons_fd = open("/dev/buttons", 0);
	if (buttons_fd < 0) {
		perror("open device buttons");
		exit(1);
	}

	char current_buttons[6];
	while(1)
	{
//		printf("current_buttons[0] = %c\n", current_buttons[0]);
//		printf("buttons[0] = %c\n", buttons[0]);
		if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
			perror("read buttons:");
			exit(1);
		}
//		printf("current_buttons[0] = %c\n", current_buttons[0]);
//		printf("buttons[0] = %c\n", buttons[0]);

		if (buttons[0] != current_buttons[0]) {
			buttons[0] = current_buttons[0];
			if(buttons[0] == '1'){
		 		printf("fuck down!\n");
				* pckey = 1;break;
			}
//			else
//				break;
		}

//		if(current_buttons[0] == '1'){
//			current_buttons[0] = '0';
//			break;
//		}

		// if meas stopped, just exit
/*
		Ctrl* p = Ctrl::getInstance();
		if(!p->getMeasState()){
			close(buttons_fd);
			return -1;
		}
*/
		if(!getMeasStatus()){
			close(buttons_fd);
			return -1;
		}
		
	}
	close(buttons_fd);
	return 0;
}

